<?php

declare(strict_types=1);

namespace App\Chat\Controller;


use App\Chat\Constant\ResponseCode;
use App\Chat\Exception\ValidateException;
use App\Common\Constants\ErrorCode;
use App\Exception\BusinessException;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Mine\MineController;

class ChatBaseController extends MineController
{
    #[Inject]
    protected ValidatorFactoryInterface $validationFactory;

    public function uid(): int
    {
        return chatUser()->getId();
    }

    protected function validate(...$arg)
    {
        $validator = di()->get(ValidatorFactoryInterface::class)->make(...$arg);
        if ($validator->fails()) {
            throw new ValidateException($validator->errors()->first(), ResponseCode::VALIDATION_ERROR);
        }
    }

    public function validateParam(array $valid_method = [],array $message = [],$code = ErrorCode::PARAMS_ERROR)
    {
        $validator = $this->validationFactory->make(
            $this -> request -> all(),
            $valid_method,
            $message
        );

        if ($validator->fails()){
            // Handle exception
            $errorMessage = $validator->errors()->first();
            $validator -> errors();
            throw new BusinessException($code,$errorMessage);
        }
    }
}